<template>
  <div>
    <el-form  @submit.native.prevent>
      <el-form-item>
        <div style="display: flex">
          <el-input v-model="input" prefix-icon="el-icon-search" placeholder="请输入" style="width:20%">
          </el-input>
          <el-button type="primary" @click="get()">查询 </el-button>
          <el-button type="primary" @click="add()"> 增加 </el-button>
          <el-button type="primary" @click="deleteSelection">
            批量删除
          </el-button>
        </div>
      </el-form-item>
    </el-form>

    <!--表格内容-->
    <el-table ref="word" :data="tableData" style="width: 100%; margin-bottom: 20px" border highlight-current-row
      :default-sort="{ prop: 'date', order: 'descending' }" @select-all="handleCheckedAllAndCheckedNone"
      @select="handleCheckedAllAndCheckedNone" @selection-change="handleSelectionChange"
      :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
      <el-table-column type="selection" fixed></el-table-column>
      <el-table-column prop="name" label="常用语/评估目的"></el-table-column>
      <el-table-column prop="type" label="常用语类型/业务类型"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="rowEdit(scope.row)" style="margin-right: 10px">编辑</el-button>
          <el-button type="text" style="color: red; margin-left: 10px" @click="rowDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination :page-sizes="[10, 20, 30, 40, 100]" :page-size="pageSize" :total="total"
      @current-change="currentChange" @size-change="sizeChange" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <Add ref="wordAdd" v-if="isShowAddDialog" :isShowAddDialog="isShowAddDialog" @dialogClose="dialogClose"
      @close="CyyList"></Add>
    <Edit ref="wordEdit" v-if="isShowEditDialog" :isShowEditDialog="isShowEditDialog" :record="formFileds"
      @dialogClose="dialogClose" @close="CyyList">
    </Edit>
  </div>
</template>

<script>
import Add from "./wordAdd.vue";
import Edit from "./wordEdit.vue";
import {
  deleteCyyById,
  getCyyWithPage,
  deleteBatch,
} from "@/api/evaluation/word";

export default {
  name: "Table",
  components: {
    Add,
    Edit,
  },
  data () {
    return {
      text: "",
      formFileds: {},
      multipleSelection: null,
      tableData: [],
      isShowAddDialog: false,
      isShowEditDialog: false,
      pageSize: 10,
      pageNum: 1,
      total: "",
      input: "",
    };
  },
  methods: {
    handleCheckedAllAndCheckedNone (selection) {
      // 当前选中仅一行时操作-（当前表格行高亮）
      1 != selection.length && this.$refs.list.setCurrentRow();
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    dialogClose () {
      this.isShowAddDialog = false;
      this.isShowEditDialog = false;
      this.get();
    },
    add () {
      this.isShowAddDialog = true;
    },
    rowEdit (row) {
      this.formFileds = row;
      this.isShowEditDialog = true;
    },
    // 删除
    rowDel (id) {
      // 让当前删除按钮失焦
      // event.target.blur();
      this.$confirm("确定要删除该账号吗？", "删除", {
        comfirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        deleteCyyById("id=" + id).then((res) => {
          if (res) {
            this.$message.success("删除成功！");
            this.get();
          }
        });
      });
    },
    // 批量删除
    deleteSelection () {
      if (!this.multipleSelection) {
        this.$message.error("请至少选择一条数据进行删除！");
        return;
      } else {
        this.$confirm("数据将会永久删除，是否继续？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }).then(() => {
          //生命空数组，这里的数组是后端需要的数据
          let array = [];
          //遍历勾选数据取自己需要的id等 这里是和后端需要的参数对应
          for (let i = 0; i < this.multipleSelection.length; i++) {
            let obj = this.multipleSelection[i].id;
            array.push(obj); //把取出来的对象push到数组里
          }
          let obj = { id: array };
          deleteBatch(obj).then((res) => {
            //接口
            this.get(); //删除完成后初始化查询列表
          });
        });
      }
    },
    // 选中当前行-当前行的复选框被勾选
    setCurRowChecked (row) {
      this.$refs.word.clearSelection();
      this.$refs.word.toggleRowSelection(row);
    },
    // 查询全部常用语，分页
    get () {
      var par;
      if (this.input) {
        par = {
          input: this.input,
          pageSize: 10,
          pageNum: 1,
        };
        this.pageSize = 10;
        this.pageNum = 1;
      } else {
        par = {
          input: this.input,
          pageSize: this.pageSize,
          pageNum: this.pageNum,
        };
      }
      getCyyWithPage(par).then((res) => {
        if (res) {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    // 显示当前页
    currentChange (e) {
      this.pageNum = e;
      this.get();
    },
    // 显示页面显示多少行
    sizeChange (e) {
      this.pageSize = e;
      this.get();
    },
  },
  mounted () {
    this.get();
  },
};
</script>

<style scoped lang="less">
.el-form {
  padding: 0 10px;
}

.el-date-editor {
  width: 100% !important;
}
</style>
